$(function () {
    var page = parseInt($(".page").val());
    //初始化分页数据
    $.ajax({
        type: "GET",
        url: "datalist",
        data: {"form": 1},
        dataType: "json",
        success: function (data) {
            data1 = data;
            /*数据不够4条隐藏按钮*/
            if (data1.Page.pageList.length < data1.Page.pageSize || data1.Page.hasNextPage == false) {
                $("#more").hide();
            } else {
                $(".page").val(page + 1);//记录页码
            }
            insertDiv(data1);
        }
    });

    //点击加载数据
    $("#more").click(function () {
        $(this).data("status", 1);
        var page = parseInt($(".page").val());
        $(this).html("加载中...");
        status = $(this).data('status');
        if (status == 1) {
            status = $(this).data("status", 0);
            $.ajax({
                type: "GET",
                url: "datalist",
                data: {"form": page},
                dataType: "json",
                success: function (data) {
                    data1 = data;
                    /*数据不够4条隐藏按钮*/
                    if (data1.Page.pageList.length < data1.Page.pageSize || data1.Page.hasNextPage == false) {
                        $("#more").hide();
                    } else {
                        $(".page").val(page + 1);//记录页码
                    }
                    insertDiv(data1);
                }
            });
        }

    });

    function insertDiv(data) {

        var information = $(".information");
        var html1;
        var datalist = data1.Page.pageList;
        for (var i = 0; i < datalist.length; i++) {
            html1 = $("<tr><td>" + datalist[i].typeIdentify + "</td><td>" + datalist[i].typeName + "</td><td>" + datalist[i].typeCreatedate + "</td><td><button type='button' class='btn btn-success btn-sm btn-rounded m-b-2 m-l-5'><a  class='typeinfo' data-tid=" + datalist[i].typeId + " style='color: white;'>查看</a></button><button type='button' class='btn btn-success btn-sm btn-rounded m-b-2 m-l-5'><a  class='typeupdate' data-tid=" + datalist[i].typeId + " style='color: white;'>修改</a></button><button type='button' class='btn btn-sm btn-rounded m-b-2 dels' style='background: #d01d13d6;'><a class='typedel' data-tid=" + datalist[i].typeId + "  style='color: white;'>删除</a></button></td></tr>");

            //绑定查看点击事件
            html1.find('.typeinfo').bind("click", function () {
                var id = $(this).data('tid');
                location.href = "datainfo?tid=" + id;
            });

            //绑定修改点击事件 
            html1.find('.typeupdate').bind("click", function () {
                var id = $(this).data('tid');
                location.href = "dataup?tid=" + id;
            });


            //绑定删除点击事件
            html1.find('.typedel').bind("click", function () {
                var id = $(this).data('tid');
                swal({
                        title: "您确定要删除该分类吗？",
                        animation: "slide-from-top",
                        showCancelButton: true,
                        closeOnConfirm: false,
                        cancelButtonText: "取消",
                        confirmButtonText: "确定"
                    },
                    function (inputValue) {
                        if (inputValue === false) {
                            return false;
                        } else {
                            $.ajax({
                                url: "datadel",
                                type: "GET",
                                data: {"tid": id},
                                datatype: "JSON",
                                success: function (data) {
                                    if (data) {
                                        swal({
                                            title: "删除成功,<span style='color:red'>已删除</span>该分类",
                                            html: true,
                                            type: "success",
                                            showCancelButton: false,
                                            showConfirmButton: true,
                                            confirmButtonText: "确定",
                                        }, function () {
                                            location.reload();
                                        });
                                    } else {

                                    }
                                },
                                error: function (data) {
                                    swal({
                                        title: '删除失败！',
                                        type: 'error',
                                        confirmButtonText: '确定',
                                        closeOnConfirm: false
                                    });
                                }
                            });
                        }
                    });
            });

            //添加数据
            information.append(html1);
        }


        $("#more").html("加载更多");
        $("#more").attr("data-status", "1");
    }


});

